package com.yy.tech.inte.service.initializer;

import com.yy.tech.inte.context.Content;
import com.yy.tech.inte.service.wrapper.HttpRequestWrapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * technical-identification$
 *
 * @author YY
 * @version 1.0.0
 * @date $ $
 * @description: TODO
 */
public abstract class AbstractHttpRequestInitializer implements HttpRequestInitializer{
    @Autowired
    private Environment env;

    @Autowired(required = false)
    protected List<RequestAuthorizer> authorizers;


    @Override
    public <T, S> void initialize(final HttpRequestWrapper<T, S> wrapper) {
        initializeRequestUrl(wrapper,getApiPrefix());
        initializeHttpHeaders(wrapper.getHeaders());
        initializeAuthorization(wrapper);
    }

    /**
     * 构建properties的key，并返回value
     * @param prefix
     * @param name
     * @return
     */
    protected String getProperty(final String prefix, final String name) {
        final StringBuilder sb = new StringBuilder();
        if (!StringUtils.isEmpty(prefix)) {
            sb.append(prefix);
            if (!prefix.endsWith(".")) {
                sb.append(".");
            }
        }
        if (!StringUtils.isEmpty(name)) {
            sb.append(name);
        }
        final String key = sb.toString();
        if (StringUtils.isEmpty(key)) {
            return null;
        }
        return env.getProperty(key,name);
    }

    protected  <T, S> void initializeRequestUrl(final HttpRequestWrapper<T, S> wrapper, String apiPrefix) {
        String host = getProperty(apiPrefix, wrapper.getTargetType());
        String uri = getProperty(null,wrapper.getUri());
        String url = RequestPathUtils.buildPath(host, uri, wrapper.getUriVariables());
        wrapper.setRequestUrl(url);
    }

    /**
     * 初始化HttpHeaders
     * @param httpHeaders
     */
    protected void initializeHttpHeaders(HttpHeaders httpHeaders){

    }


    /**
     * 初始化HttpRequest的Authorization
     * @param wrapper
     * @param <T>
     * @param <S>
     */
    protected <T,S> void initializeAuthorization(HttpRequestWrapper<T,S> wrapper){
        if (!CollectionUtils.isEmpty(authorizers)){
            authorizers.forEach(a->{
                if (a.support(wrapper.getAuthType(), wrapper.getTargetType())){
                    a.authorize(wrapper);
                }
            });
        }
    }

    protected String getApiPrefix(){
        return null;
    }


    @Component
    @Order(Content.INT_ORDER_DEFAULT)
    public static class Impl extends AbstractHttpRequestInitializer{

        @Override
        public <T, S> boolean support(final HttpRequestWrapper<T, S> wrapper) {
            return true;
        }

        @Override
        protected String getApiPrefix() {
            return Content.API_TARGET_PREFIX;
        }
    }

}
